package com.fjeld.middleware.jaxb;

import static com.fjeld.middleware.jaxb.V1Message.CorrespondenceStatus;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 01/01/2012
 * Time: 22:45
 * <p/>
 * <p/>
 * <p>Java class for anonymous complex type.
 * <p/>
 * <p>The following schema fragment specifies the expected content contained within this class.
 * <p/>
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{com.fjeld.middleware.jaxb}V1_Message" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "v1Messages"
})
@XmlRootElement(name = "V1_MessageAuditTrail")
public class V1MessageAuditTrail
{

    @XmlElement(name = "V1_Message", required = true)
    protected List<V1Message> v1Messages;
    @XmlAttribute(name = "CorrespondenceType", required = true)
    protected String correspondenceType;
    @XmlAttribute(name = "CorrespondenceID", required = true)
    protected String correspondenceID;
    @XmlAttribute(name = "CorrespondenceStatus", required = true)
    protected String correspondenceStatus;
    @XmlAttribute(name = "SendingTime")
    protected Long sendingTime;
    @XmlAttribute(name = "ProcessingStartTime")
    protected Long processingStartTime;
    @XmlAttribute(name = "ProcessingEndTime")
    protected Long processingEndTime;
    @XmlAttribute(name = "ProcessingLatency")
    protected Long processingLatency;
    @XmlAttribute(name = "LastUpdateTime")
    protected Long lastUpdateTime;
    @XmlAttribute(name = "ReceiveTime")
    protected Long receiveTime;
    @XmlAttribute(name = "Latency")
    protected Long latency;

    public V1MessageAuditTrail()
    {
        v1Messages = new ArrayList<V1Message>(3); // request, ack, response
    }

    public V1MessageAuditTrail(V1Message v1Message)
    {
        v1Messages = new ArrayList<V1Message>(3); // request, ack, response
        correspondenceID = v1Message.getCorrespondenceID();
        correspondenceStatus = v1Message.getCorrespondenceStatus();
        correspondenceType = v1Message.correspondenceType;
        sendingTime = v1Message.getSendingTime();
        lastUpdateTime = new Long(0);
        processingStartTime = new Long(0);
        processingEndTime = new Long(0);
        processingLatency = new Long(0);
        receiveTime = new Long(0);
        latency = new Long(0);
        v1Messages.add(v1Message);
    }

    /**
     * Gets the value of the getV1Messages property.
     * <p/>
     * <p/>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the v4Class property.
     * <p/>
     * <p/>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getV1Messages().add(newItem);
     * </pre>
     * <p/>
     * <p/>
     * <p/>
     * Objects of the following type(s) are allowed in the list
     * {@link V1Message }
     */
    public List<V1Message> getV1Messages()
    {
        return this.v1Messages;
    }

    public String getCorrespondenceID()
    {
        return correspondenceID;
    }

    public void setCorrespondenceID(String correspondenceID)
    {
        this.correspondenceID = correspondenceID;
    }

    public String getCorrespondenceStatus()
    {
        return correspondenceStatus;
    }

    public void setCorrespondenceStatus(String correspondenceStatus)
    {
        this.correspondenceStatus = correspondenceStatus;
    }

    public String getCorrespondenceType()
    {
        return correspondenceType;
    }

    public void setCorrespondenceType(String correspondenceType)
    {
        this.correspondenceType = correspondenceType;
    }

    public Long getSendingTime()
    {
        return sendingTime;
    }

    public void setSendingTime(Long sendingTime)
    {
        this.sendingTime = sendingTime;
    }

    public Long getLastUpdateTime()
    {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Long lastUpdateTime)
    {
        this.lastUpdateTime = lastUpdateTime;
    }

    public Long getReceiveTime()
    {
        return receiveTime;
    }

    public void setReceiveTime(Long receiveTime)
    {
        this.receiveTime = receiveTime;
    }

    public void update(V1Message v1Message)
    {
        correspondenceStatus = v1Message.getCorrespondenceStatus();
        lastUpdateTime = v1Message.getSendingTime();

        switch (V1Message.CorrespondenceStatus.valueOf(correspondenceStatus))
        {
            case Acknowledged:
                receiveTime = v1Message.receiveTime;
                latency = receiveTime - sendingTime;
                break;
            case Processing:
                processingStartTime = v1Message.getCreateTime();
                break;
            case Accepted:
            case Rejected:
                processingEndTime = v1Message.getCreateTime();
        }

        if (0 != processingStartTime && 0 != processingEndTime)
        {
            processingLatency = processingEndTime - processingStartTime;
        }

        v1Messages.add(v1Message);
    }

    public long getLatency()
    {
        return latency;
    }

    public void setLatency(Long latency)
    {
        this.latency = latency;
    }

    public Long getProcessingStartTime()
    {
        return processingStartTime;
    }

    public void setProcessingStartTime(Long processingStartTime)
    {
        this.processingStartTime = processingStartTime;
    }

    public Long getProcessingEndTime()
    {
        return processingEndTime;
    }

    public void setProcessingEndTime(Long processingEndTime)
    {
        this.processingEndTime = processingEndTime;
    }

    public Long getProcessingLatency()
    {
        return processingLatency;
    }

    public void setProcessingLatency(Long processingLatency)
    {
        this.processingLatency = processingLatency;
    }
}